/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package src.report.accounting;

import java.io.IOException;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import src.DBManager;
import src.domain.gl.Fstmt;
import src.domain.gl.FstmtReport;
import src.domain.system.Branch;
import src.report.AbstractBaseReportBean;
import src.services.CRUEDService;
import src.util.Constanta;
import src.view.system.CompanyBean;

/**
 *
 * @author Lalugina
 */
@ManagedBean
@SessionScoped
public class RptFstmtBean extends AbstractBaseReportBean {

    /**
     * Creates a new instance of RptFstmtBean
     */
    
    private final int REPORT_SIMPLE = 1;
    private final int REPORT_COMPARE = 2;
    private final String COMPILE_FILE_NAME_SIMPLE = "gl/fstmtsimple";
    private final String COMPILE_FILE_NAME_COMPARED = "gl/fstmtcompare";
    
    private List<FstmtReport> fstmtReports;
    private Integer branchId, year, month, fstmtId, reportId;
     
    public RptFstmtBean() {
    }

    @Override
    protected JRDataSource getJRDataSource() {
        RptFstmtDS ds = new RptFstmtDS(fstmtReports);
        return ds;
    }

    @Override
    protected String getCompileFileName() {
        if (reportId==1) return COMPILE_FILE_NAME_SIMPLE;
        else  return COMPILE_FILE_NAME_COMPARED;
    }

    public Integer getBranchId() {
        return branchId;
    }

    public void setBranchId(Integer branchId) {
        this.branchId = branchId;
    }

    public List<FstmtReport> getFstmtReports() {
        return fstmtReports;
    }

    public void setFstmtReports(List<FstmtReport> fstmtReports) {
        this.fstmtReports = fstmtReports;
    }

    public Integer getMonth() {
        return month;
    }

    public void setMonth(Integer month) {
        this.month = month;
    }

    public Integer getYear() {
        return year;
    }

    public void setYear(Integer year) {
        this.year = year;
    }

    public Integer getFstmtId() {
        return fstmtId;
    }

    public void setFstmtId(Integer fstmtId) {
        this.fstmtId = fstmtId;
    }

    public Integer getReportId() {
        return reportId;
    }

    public void setReportId(Integer reportId) {
        this.reportId = reportId;
    }
    
    public String execute(List<FstmtReport> reports) {
	try {
            String branchName = "Semua Cabang";
            if (branchId>0) branchName = ((Branch) new CRUEDService().getById(Branch.class, branchId)).getBranchName();
            
            Fstmt fstmt = (Fstmt) new CRUEDService().getById(Fstmt.class, fstmtId);
            String rptCategory = null; 
            Integer lastMonth = month -1;
            Integer lastYear = year;
            if (lastMonth==0) {
                lastMonth = 12;
                lastYear = year-1;
            }
            String colcaption = src.util.Utils.getMonthName(month).concat(" ").concat(year.toString());
            String col02caption = null;// = src.util.Utils.getMonthName(month).concat(" ").concat(lastYear.toString());
            
            reportId = REPORT_COMPARE;
            switch (fstmt.getFormat()) {
                case Constanta.FSTMT_FORMAT_NET_CHANGE_SIMPLE: 
                    rptCategory = "Simple"; 
                    reportId = REPORT_SIMPLE;
                    break;
                case Constanta.FSTMT_FORMAT_BALANCE_SIMPLE: 
                    rptCategory = "Simple"; 
                    reportId = REPORT_SIMPLE;
                    break;
                case Constanta.FSTMT_FORMAT_NET_CHANGE_COMPARE_MONTH: 
                    rptCategory = "Compare Month"; 
                    col02caption= src.util.Utils.getMonthName(lastMonth).concat(" ").concat(lastYear.toString());
                    break;
                case Constanta.FSTMT_FORMAT_NET_CHANGE_COMPARE_YEAR: 
                    Integer tmpYear = year -1;
                    rptCategory = "Compare Year"; 
                    col02caption= src.util.Utils.getMonthName(month).concat(" ").concat(tmpYear.toString());
                    break;
                case Constanta.FSTMT_FORMAT_BALANCE_COMPARE_YEAR: 
                    Integer tmpYear2 = year -1;
                    rptCategory = "Compare Year"; 
                    col02caption= src.util.Utils.getMonthName(month).concat(" ").concat(tmpYear2.toString());
                    break;
            }
                    
            super.getReportParameters().put("p_caption", fstmt.getFstmtName());
            super.getReportParameters().put("p_category", rptCategory);
            super.getReportParameters().put("p_company", new CompanyBean().loadCompanyInfo().toString());
            super.getReportParameters().put("p_column", colcaption);
            super.getReportParameters().put("p_column2", col02caption);
            super.getReportParameters().put("p_branch", branchName);
            
            DBManager db = new DBManager();
            db.getConnection();
            super.getReportParameters().put("REPORT_CONNECTION", db.conn);
            this.fstmtReports = reports;
            super.prepareReportViaConn(getJRDataSource());
            db.putConnection();
	} catch (JRException | IOException e) {}
	
        return null;
    }
}
